package com.szgr.baoyin.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.AddressActivity;
import com.szgr.baoyin.activity.AptitudeCheckActivity;
import com.szgr.baoyin.activity.LoginActivity;
import com.szgr.baoyin.activity.MessageCenterActivity;
import com.szgr.baoyin.activity.MyForumActivity;
import com.szgr.baoyin.activity.MyMoneyActivity;
import com.szgr.baoyin.activity.MyProtectActivity;
import com.szgr.baoyin.activity.VipCardActivity;
import com.szgr.baoyin.adapter.PersonalMessageListAdapter;
import com.szgr.baoyin.myview.NoScrollListView;
import com.szgr.baoyin.utils.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class PersonalMessageFragment extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener {
    private List<Map<String, Object>> data;
    private Handler mhandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    MyApplication.getmInstance().clearLoginMessage();
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    break;
            }
        }
    };

    public PersonalMessageFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_personal_message, container, false);
        initView(ret);
        return ret;
    }

    @Override
    public void onPause() {
        super.onPause();
        MyApplication.getmInstance().cancelPendingRequests(MyApplication.TAG);
    }

    private void initView(View ret) {
        data = new ArrayList<>();
        initData();
        NoScrollListView nslv_personalMessage = (NoScrollListView) ret.findViewById(R.id.personal_message_list_view);
        PersonalMessageListAdapter listAdapter = new PersonalMessageListAdapter(getActivity(), data);
        nslv_personalMessage.setAdapter(listAdapter);
        nslv_personalMessage.setOnItemClickListener(this);

        ret.findViewById(R.id.exit).setOnClickListener(this);
    }

    private void initData() {
        HashMap<String, Object> map = new HashMap<>();
//        map.put("icon", R.drawable.personal_01);
//        map.put("name", "绑定手机号");
//        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_05);
        map.put("name", "我的维保");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_04);
        map.put("name", "资质审核");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_03);
        map.put("name", "我的地址");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_08);
        map.put("name", "都银卡");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_07);
        map.put("name", "我的都币");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_06);
        map.put("name", "消息中心");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_02);
        map.put("name", "我的论坛");
        data.add(map);

        map = new HashMap<>();
        map.put("icon", R.drawable.personal_09);
        map.put("name", "帐户安全");
        data.add(map);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
//            case R.id.message_check:
//                startActivity(new Intent(getActivity(), AptitudeCheckActivity.class));
//                break;
//            case R.id.message_forum:
//                startActivity(new Intent(getActivity(), MyForumActivity.class));
//                break;
//            case R.id.message_weibao:
//                startActivity(new Intent(getActivity(), MyProtectActivity.class));
//                break;
//            case R.id.message_address:
//                startActivity(new Intent(getActivity(), AddressActivity.class));
//                break;
            case R.id.exit:
                exitApp();
                break;
        }
    }

    /**
     * 退出登录
     */
    private void exitApp() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_EXIT,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        System.out.println("-----exit---->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            Object datas = jsonObject.get("datas");
                            if (datas instanceof String) {
                                String result = (String) datas;
                                if("1".equals(result)) {
                                    mhandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("key",MyApplication.getmInstance().getKey());
                map.put("username",MyApplication.getmInstance().getUsername());
                map.put("client","android");
                return map;
            }
        };
        MyApplication.getmInstance().addToRequestQueue(sr);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
//            case 0://绑定手机号
//                break;
            case 0://我的维保
                startActivity(new Intent(getActivity(),MyProtectActivity.class));
                break;
            case 1://资质审核
                startActivity(new Intent(getActivity(),AptitudeCheckActivity.class));
                break;
            case 2://我的地址
                startActivity(new Intent(getActivity(),AddressActivity.class));
                break;
            case 3://都银卡
                startActivity(new Intent(getActivity(), VipCardActivity.class));
                break;
            case 4://我的都币
                startActivity(new Intent(getActivity(), MyMoneyActivity.class));
                break;
            case 5://消息中心
//                startActivity(new Intent(getActivity(),MessageCenterActivity.class));
                break;
            case 6://我的论坛
                startActivity(new Intent(getActivity(),MyForumActivity.class));
                break;
            case 7://帐户安全
                break;
        }
    }
}
